package sim1011.starter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileProcessor {
	public static void main(String[] args) {
		String politic = "cost1dtraffic1d";
		int how_manu = 10;
		String[] measure = {"global_cost", "battery_level", "mobileCpuLoad_level", "mobileMemoryLoad_level", "throughput_level", "traffic_level"};
		for (String m : measure) {
			FileProcessor p = new FileProcessor();
			p.computeFilesForMeasure(how_manu, m, politic);
		}
	}
	
	public void computeFilesForMeasure(int end, String measure, String politic) {
		String dir = "simulations/measures/"+politic+"/";
		FileWriter writer = null;
		try {
			writer = new FileWriter(new File(dir+measure+"_"+politic+"_final.txt"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		BufferedReader[] read = new BufferedReader[end];
		for (int i=0; i<end; ++i) {
			try {
				read[i] = new BufferedReader(new FileReader(dir+measure+"_"+politic+"_"+(i+1)+".txt"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		int finishedN = 0;
		boolean newLine = true;
		while (finishedN < 90) {
			for (int j=0; j<end; ++j) {
				String value;
				try {
					value = read[j].readLine();
					if (newLine == true) {
						writer.append(value);
						newLine = false;
					}
					else
						writer.append(";" + value);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				writer.append("\n");
				newLine = true;
			} catch (IOException e) {
				e.printStackTrace();
			}
			finishedN++;
		}
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int i=0; i<end; ++i) {
			try {
				read[i].close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
